module mod_random

contains


function ran1()  !returns random number between 0 - 1 
        implicit none 
        real*8 ran1,x 
        call random_number(x) ! built in fortran 90 random number function 
        ran1=x 
    end function ran1

    function spread(min,max)  !returns random number between min - max 
        implicit none 
        real*8 spread 
        real*8 min,max 
        spread=(max - min) * ran1() + min 
    end function spread

    function random_normal(mean,sigma) !returns a normal distribution 
        implicit none 
        real*8, intent(in) :: mean,sigma
        real*8 tmp, random_normal
        integer flag 
        real*8 fac,gsave,rsq,r1,r2 
        save flag,gsave 
        data flag /0/ 
        if (flag.eq.0) then 
        rsq=2.0 
            do while(rsq.ge.1.0.or.rsq.eq.0.0) ! new from for do 
                r1=2.0*ran1()-1.0 
                r2=2.0*ran1()-1.0 
                rsq=r1*r1+r2*r2 
            enddo 
            fac=sqrt(-2.0*log(rsq)/rsq) 
            gsave=r1*fac 
            tmp=r2*fac 
            flag=1 
        else 
            tmp=gsave 
            flag=0 
        endif 
        random_normal=tmp*sigma+mean 
        return 
      end function random_normal

  end module mod_random
